package org.robot.scm.platform.server.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import lombok.NoArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;

/**
 * 删除更新包
 */
@Slf4j
@NoArgsConstructor
public class RmRemoteHandler extends BaseHandler implements HttpHandler {

    @Override
    public String getUri() {
        return "/rmRemote";
    }

    @Override
    public boolean needAuth() {
        return Boolean.FALSE;
    }

    @SneakyThrows
    @Override
    public void handle(HttpExchange exchange) throws IOException {
        if (!exchange.getRequestMethod().equalsIgnoreCase("POST")) {
            sendFailureResponse(exchange, "请求非法");
            return;
        }
        Collection<File> files = FileUtils.listFiles(new File("assets/hotkit"), new String[]{"wgt"}, false);
        for (File file : files) {
            FileUtils.forceDelete(file);
        }
        sendSuccessResponse(exchange, "删除更新成功");
    }
}
